import { request } from "../../request/index.js"
import regeneratorRuntime from "../../lib/runtime/runtime.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs:[
            {
                id:0,
                value:"综合",
                isActive:true
            },
            {
                id:1,
                value:"销量",
                isActive:false
            },
            {
                id:2,
                value:"价格",
                isActive:false
            }
        ],
        goodsList:[]
    },
    queryParams:{
        query:"",
        cid:"",
        pagenum:1,
        pagesize:10
    },
    totalPages:0,
    /**
     * 子组件向父组件传递参数的处理函数
     * */ 
    handleTabsItemChange(e){
        const index = e.detail.index;
        let {tabs} = this.data;
        tabs.forEach((v,i) => {
            return i===index.index?v.isActive=true:v.isActive=false
        });
        this.setData({
            tabs
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.queryParams.cid = options.cid||"";
        this.queryParams.query = options.query||"";
        this.getGoodsList();
    },
    /**
     * 获取商品的列表信息
     * */ 
    async getGoodsList(){
        const res = await request({
            url:"/goods/search",
            data:this.queryParams
        });
        const total=res.total;
        this.totalPages=Math.ceil(total/this.queryParams.pagesize)
        this.setData({
            goodsList : [...this.data.goodsList,...res.goods]
        });
        wx.stopPullDownRefresh();
          
    },
    /**
     * 页面上滑 滚动条触底事件
     * */
    onReachBottom(){
        if(this.queryParams.pagenum>=this.totalPages){
            // console.log("没有下一页数据！")
            wx.showToast({
                title: '没有下一页数据！'
            });
              
        }else{
            this.queryParams.pagenum++;
            this.getGoodsList()
        }
    },
    /**
     * 下拉刷新事件
     * */ 
    onPullDownRefresh(){
        this.setData({
            goodsList:[]
        });
        this.queryParams.pagenum=1;
        this.getGoodsList();
    }
})